using System;
using System.Globalization;
using HestiaBackup.ExternalTools.VB.JeffAtwood.Encryption;

namespace HestiaBackup.Library.ConfigurationManager
{
    public class FtpSetting
    {
        public FtpSetting()
        {
            _ProcessDate = DateTime.Now;
            _LastRunStart = DateTime.Now;
            _LastRunSuccess = false;
        }

        private int _id;

        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        private string _FriendlyName;

        /// <summary>
        /// Get or Set a friendly name that represent the project
        /// </summary>
        public string FriendlyName
        {
            get { return _FriendlyName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _FriendlyName = value;
            }
        }

        private DateTime _ProcessDate;

        /// <summary>
        /// Get or Set a date/hour for processing the project
        /// </summary>
        public DateTime ProcessDate
        {
            get { return _ProcessDate; }
            set { _ProcessDate = value; }
        }

        private string _Server;

        /// <summary>
        /// Get or Set the server url
        /// </summary>
        public string Server
        {
            get { return _Server; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _Server = value;
            }
        }

        private string _User;

        /// <summary>
        /// Get or Set the user name
        /// </summary>
        public string User
        {
            get { return _User; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _User = value;
            }
        }

        private string _EncryptedPassword;

        public string EncryptedPassword
        {
            get { return _EncryptedPassword; }
            set
            {
                _EncryptedPassword = value;
                string EncryptionKey = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", Server, User);
                Symmetric.Provider provider = Symmetric.Provider.TripleDES;
                Symmetric encrypt = new Symmetric(provider, true);
                if (EncryptionKey.Length > 16)
                {
                    EncryptionKey = EncryptionKey.Substring(0, 16);
                }
                encrypt.Key.Text = EncryptionKey;

                ExternalTools.VB.JeffAtwood.Encryption.Data encryptedData;
                encryptedData = encrypt.Decrypt(new ExternalTools.VB.JeffAtwood.Encryption.Data(_EncryptedPassword));
                _Password = encryptedData.Text;
            }
        }

        private string _Password;

        /// <summary>
        /// Get or Set the password
        /// </summary>
        public string Password
        {
            get { return _Password; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _Password = value;
                string EncryptionKey = string.Format(CultureInfo.InvariantCulture, "{0}-{1}", Server, User);
                Symmetric.Provider provider = Symmetric.Provider.TripleDES;
                Symmetric encrypt = new Symmetric(provider, true);
                if (EncryptionKey.Length > 16)
                {
                    EncryptionKey = EncryptionKey.Substring(0, 16);
                }
                encrypt.Key.Text = EncryptionKey;

                ExternalTools.VB.JeffAtwood.Encryption.Data encryptedData;
                encryptedData = encrypt.Encrypt(new ExternalTools.VB.JeffAtwood.Encryption.Data(_Password));
                _EncryptedPassword = encryptedData.Text;
            }
        }

        private DateTime _LastRunStart;

        /// <summary>
        /// Get or Set the last run starting date
        /// </summary>
        public DateTime LastRunStart
        {
            get { return _LastRunStart; }
            set { _LastRunStart = value; }
        }

        private DateTime _LastRunEnd;

        /// <summary>
        /// Get or Set a date/hour for processing the project
        /// </summary>
        public DateTime LastRunEnd
        {
            get { return _LastRunEnd; }
            set { _LastRunEnd = value; }
        }

        private bool _LastRunSuccess;

        /// <summary>
        /// Get or Set the result of last run
        /// </summary>
        public bool LastRunSuccess
        {
            get { return _LastRunSuccess; }
            set { _LastRunSuccess = value; }
        }

        private bool _Enabled;

        /// <summary>
        /// Get or Set the result of last run
        /// </summary>
        public bool Enabled
        {
            get { return _Enabled; }
            set { _Enabled = value; }
        }

        private bool _IsRunning;

        public bool IsRunning
        {
            get { return _IsRunning; }
            set { _IsRunning = value; }
        }

        private bool _KeepDeletedFiles;

        /// <summary>
        /// Get or Set if the deleted files must be keeped
        /// </summary>
        public bool KeepDeletedFiles
        {
            get { return _KeepDeletedFiles; }
            set { _KeepDeletedFiles = value; }
        }

        private string _RemoteFolder;

        /// <summary>
        /// Get or Set the remote folder
        /// </summary>
        public string RemoteFolder
        {
            get
            {
                if (string.IsNullOrEmpty(_RemoteFolder))
                {
                    return "/";
                }
                if (!_RemoteFolder.StartsWith("/"))
                {
                    _RemoteFolder = "/" + _RemoteFolder;
                }
                return _RemoteFolder.TrimEnd('/');
            }
            set { _RemoteFolder = value; }
        }

        private string _LocalFolder;

        /// <summary>
        /// Get or Set a friendly name that represent the project
        /// </summary>
        public string LocalFolder
        {
            get { return _LocalFolder; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _LocalFolder = value;
            }
        }

        private string _LocalDeletedFolder;

        /// <summary>
        /// Get or Set a friendly name that represent the project
        /// </summary>
        public string LocalDeletedFolder
        {
            get { return _LocalDeletedFolder; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _LocalDeletedFolder = value;
            }
        }

        private string _LogFileName;

        /// <summary>
        /// Get or Set a friendly name that represent the project
        /// </summary>
        public string LogFileName
        {
            get { return _LogFileName; }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("Value cannot be null");
                }
                _LogFileName = value;
            }
        }
    }
}